﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Services;
using Pg.BioMedics.SDR.Web.Controls;
using Pg.BioMedics.SDR.Web.Constants;

public partial class Administration_Views_PersonDetailsView : BaseHostedView
{
    #region Fields

    private IManagementService managementService;

    private Person person = new Person();
    private PlayedRole playedRole = new PlayedRole();

    #endregion

    #region Properties

    /// <summary>
    /// Sets managment service instance. Utilized by IoC context for 
    /// managment service delivery to this view
    /// </summary>
    public IManagementService ManagmentService
    {
        set
        {
            this.managementService = value;
        }
    }

    /// <summary>
    /// Gets person
    /// </summary>
    public Person Person
    {
        get
        {
            return this.person;
        }
    }

    /// <summary>
    /// Gets played role
    /// </summary>
    public PlayedRole PlayedRole
    {
        get
        {
            return this.playedRole;
        }
    }

    #endregion

    #region Event handlers

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.person != null)
        {
            if (this.person.Id.HasValue)
            {
                this.playedRole = this.managementService.GetPlayedRoles(this.person).FirstOrDefault();
                this.playedRole = this.playedRole ?? new PlayedRole();
            }
        }
    }

    protected override void LoadParameters(object[] parameters)
    {
        if (parameters.Length == 1)
        {
            if (parameters.First() is Person)
                this.person = parameters.First() as Person;
        }
    }

    protected void returnLinkButton_Click(object sender, EventArgs e)
    {
        base.Return();
    }

    protected void saveLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            base.UnbindFormData();

            this.managementService.Update(person, playedRole);
            base.Return();
        }
        catch (Exception ex)
        {
            base.Navigate(UsersManagementViews.ErrorView, ex);
        }
    }

    protected void activateLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            base.UnbindFormData();

            this.managementService.Activate(this.person);
            base.Return();
        }
        catch (Exception ex)
        {
            base.Navigate(UsersManagementViews.ErrorView, ex);
        }
    }

    protected void setPasswordLinkButton_Click(object sender, EventArgs e)
    {
        base.UnbindFormData();

        base.Navigate(UsersManagementViews.SetPasswordView, UsersManagementViews.PersonDetailsView, this.person);
    }

    protected void disableLinkButton_Click(object sender, EventArgs e)
    {
        try
        {
            base.UnbindFormData();

            this.managementService.Disable(this.person);
            base.Return();
        }
        catch (Exception ex)
        {
            base.Navigate(UsersManagementViews.ErrorView, ex);
        }
    }

    protected void changePasswordLinkButton_Click(object sender, EventArgs e)
    {
        base.UnbindFormData();
        base.Navigate(UsersManagementViews.SetPasswordView, UsersManagementViews.PersonDetailsView, this.person);
    }

    #endregion
}
